import { c } from "../../tool/core";
import { cryptPwd } from "../../tool/toll";
let fs = require("fs");
import { guzhuang_insert } from "../../db/insert";

export async function fn(uri) {
  function callback(res, $) {
    let nodeList = $(".item-num");
    let name: string = "";
    let url: string = "";
    let season: string = "";
    let title = "";
    let pwd = "";
    let flag: String = "0.0.0.i1"; //完整分集片源
    let prew: String = "0.0.0.i3"; //预览
    let tmpType: String = "";
    let type: Number = 1;
    let ykID = "";
    title = $("meta[name=irAlbumName]").attr("content");
    pwd = cryptPwd(title, type);
    Object.keys(nodeList).forEach((key, index) => {
      name = $(nodeList[key]).attr("title");
      season = $(nodeList[key])
        .children("a")
        .eq(0)
        .children("span")
        .eq(1)
        .text();
      tmpType = $(nodeList[key])
        .children("a")
        .eq(0)
        .children("span")
        .eq(0)
        .data("spm-anchor-id");
      url = $(nodeList[key])
        .children("a")
        .attr("href");

      //检出所有完整视频源
      if ((!tmpType || tmpType === flag) && name) {
        url = url.split("?")[0];
        ykID = url.split("id_")[1].substr(0, url.split("id_")[1].length - 5);

        let arr = [title, pwd, name, season, url, type, ykID].map(el => {
          return `'${el}'`;
        });

        guzhuang_insert("gu_zhuang", arr);
        // console.log([title, pwd, name, season, url, type]);
      }
    });

    // fs.writeFileSync("./九州缥缈录.html", nodeList, "utf-8");
  }
  c(uri, callback);

  console.log("哈哈哈哈哈");
}
